﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Web;

namespace AgileShare.Framework.Library.Helpers
{
    public static class SPStringExtensions
    {

        public static string TimeAgo(this DateTime date)
        {

            TimeSpan timeSince = DateTime.Now.Subtract(date);



            if (timeSince.TotalMilliseconds < 1)

                return "not yet";



            if (timeSince.TotalMinutes < 1)

                return "just now";

            if (timeSince.TotalMinutes < 2)

                return "1 minute ago";

            if (timeSince.TotalMinutes < 60)

                return string.Format("{0} minutes ago", timeSince.Minutes);

            if (timeSince.TotalMinutes < 120)

                return "1 hour ago";

            if (timeSince.TotalHours < 24)

                return string.Format("{0} hours ago", timeSince.Hours);

            if (timeSince.TotalDays == 1)

                return "yesterday";

            if (timeSince.TotalDays < 7)

                return string.Format("{0} days ago", timeSince.Days);

            if (timeSince.TotalDays < 14)

                return "last week";

            if (timeSince.TotalDays < 21)

                return "2 weeks ago";

            if (timeSince.TotalDays < 28)

                return "3 weeks ago";

            if (timeSince.TotalDays < 60)

                return "last month";

            if (timeSince.TotalDays < 365)

                return string.Format("{0} months ago", Math.Round(timeSince.TotalDays / 30));

            if (timeSince.TotalDays < 730)

                return "last year";

            //last but not least...

            return string.Format("{0} years ago", Math.Round(timeSince.TotalDays / 365));

        }
        public static string SubstringAfter(string source, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return source;
            }
            CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
            int index = compareInfo.IndexOf(source, value, CompareOptions.Ordinal);
            if (index < 0)
            {
                //No such substring
                return string.Empty;
            }
            return source.Substring(index + value.Length);
        }

        public static string SubstringBefore(string source, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return value;
            }
            CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
            int index = compareInfo.IndexOf(source, value, CompareOptions.Ordinal);
            if (index < 0)
            {
                //No such substring
                return string.Empty;
            }
            return source.Substring(0, index);
        }

        /// <summary>
        /// The replace ex.
        /// </summary>
        /// <param name="original">
        /// The original.
        /// </param>
        /// <param name="pattern">
        /// The pattern.
        /// </param>
        /// <param name="replacement">
        /// The replacement.
        /// </param>
        /// <returns>
        /// The replace ex.
        /// </returns>
        public static string ReplaceEx(string original, string pattern, string replacement)
        {
            int count, position0, position1;
            count = position0 = position1 = 0;
            string upperString = original.ToUpper();
            string upperPattern = pattern.ToUpper();
            int inc = (original.Length / pattern.Length) * (replacement.Length - pattern.Length);
            char[] chars = new char[original.Length + Math.Max(0, inc)];
            while ((position1 = upperString.IndexOf(upperPattern, position0)) != -1)
            {
                for (int i = position0; i < position1; ++i)
                {
                    chars[count++] = original[i];
                }

                for (int i = 0; i < replacement.Length; ++i)
                {
                    chars[count++] = replacement[i];
                }

                position0 = position1 + pattern.Length;
            }

            if (position0 == 0)
            {
                return original;
            }

            for (int i = position0; i < original.Length; ++i)
            {
                chars[count++] = original[i];
            }

            return new string(chars, 0, count);
        }

        /// <summary>
        /// Finds a given string and enclosing it with tags provided.
        /// </summary>
        /// <remarks>
        /// the search is Case Insensitive
        /// </remarks>
        /// <param name="sourceString">
        /// The source string.
        /// </param>
        /// <param name="findString">
        /// The find string.
        /// </param>
        /// <param name="startTag">
        /// The start tag.
        /// </param>
        /// <param name="endTag">
        /// The end tag.
        /// </param>
        /// <returns>
        /// The tag string.
        /// </returns>
        public static string TagString(string sourceString, string findString, string startTag, string endTag)
        {
            if (sourceString.IndexOf(findString, StringComparison.InvariantCultureIgnoreCase) == -1)
            {
                return sourceString;
            }

            string newString;

            // If this is being called from a Web site, lets Decode 
            if (HttpContext.Current != null)
            {
                newString = HttpContext.Current.Server.HtmlDecode(sourceString);
            }
            else
            {
                newString = sourceString;
            }

            for (int i = 0; i < newString.Length; i++)
            {
                int pos = newString.IndexOf(findString, i, StringComparison.InvariantCultureIgnoreCase);

                // if she isnt found return original string
                if (pos == -1)
                {
                    return newString;
                }

                int newPos = pos + startTag.Length + findString.Length;
                newString = newString.Insert(pos, startTag);
                newString = newString.Insert(newPos, endTag);
                i = newPos + endTag.Length;
            }

            return sourceString;
        }

        public static string TrimString(string value, int maxLength)
        {
            // If it is smaller than the max len then return the string without doing anything
            if (value == null || value.Length <= maxLength)
                return value;

            return value.Substring(0, 100) + "...";

        }

        public static string Cut(this string s, int l, string endStr)
        {
            string temp = s.Substring(0, (s.Length < l + 1) ? s.Length : l + 1);
            byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(temp);

            string outputStr = "";
            int count = 0;

            for (int i = 0; i < temp.Length; i++)
            {
                if ((int)encodedBytes[i] == 63)
                    count += 2;
                else
                    count += 1;

                if (count <= l - endStr.Length)
                    outputStr += temp.Substring(i, 1);
                else if (count > l)
                    break;
            }

            if (count <= l)
            {
                outputStr = temp;
                endStr = "";
            }

            outputStr += endStr;

            return outputStr;
        }
    }
}
